package v1

import "fmt"

type Router struct {
	nodes map[string]*Node
}

type Node struct {
	path    string
	handler HandleFunc
	route   string
	method  string
}

func newRouter() Router {
	return Router{
		nodes: map[string]*Node{},
	}
}

func (r *Router) addRoute(method string, path string, handler HandleFunc) {

	key := method + "_" + path
	_, ok := r.nodes[key]
	if !ok {
		nodeTemp := &Node{
			path:    path,
			method:  method,
			handler: handler,
		}
		r.nodes[key] = nodeTemp
	} else {
		panic("路由重复")
	}

}

func (r *Router) addRouteV2(method string, path string, handler HandleFunc) {

	key := method + "_" + path
	_, ok := r.nodes[key]
	if !ok {
		nodeTemp := &Node{
			path:    path,
			method:  method,
			handler: handler,
		}
		r.nodes[key] = nodeTemp
	} else {
		panic("路由重复")
	}

}

func (r *Router) get(key string) *Node {
	n, ok := r.nodes[key]
	if ok {
		return n
	} else {
		fmt.Println("路由不存在")
		return nil
	}
}
